# 字符串CRUD操作练习，文件操作练习
# 注意：字符串本身不可变，下面操作方法都是会返回新的字符串
# 增加（create）
a = 'a'
b = 'b'
a += b
print(a)  # 'ab'

# 查找（retrieve）
hello = 'hello world'
print(hello.find('h'))  # 0
print(hello.find('q'))  # -1
print(hello.index('h'))  # 0
# print(hello.index('q'))  # 直接报错ValueError: substring not found

# 修改（update）
print(hello.replace('h', 'H'))  # 'Hello world'
date_time = '2020-10-25 11:47:00'
print(date_time.split(' '))  # ['2020-10-25', '11:47:00']
tmp = ' 111 '
print(tmp.join('567'))  # '5 111 6 111 7'

# 删除（delete）
txt = '  This a test text!!  '
print(txt.strip())  # 'This a test text!!'
print(txt.lstrip())  # 'This a test text!!  '
print(txt.rstrip())  # '  This a test text!!'
txt2 = '[temp]'
print(txt2.strip('[]'))  # 'temp'

# 字符串格式化操作
a = '2020-10-%s %s:%s:%s' % (25, 12, '01', 54)
b = '2020-10-{} {}:{}:{}'.format(25, 12, '01', 54)
c = '2020-10-{0} {1}:{2}:{2}'.format(25, 12, 35)
d = '2020-10-{day} {hour}:{min}:{sec}'.format(day=25, hour=12, sec=55, min='05')
day, hour, minute, sec = 25, 12, '06', 45
e = f'2020-10-{day} {hour}:{minute}:{sec}'
print(a, b, c, d, e, sep='\n')
'''
2020-10-25 12:01:54
2020-10-25 12:01:54
2020-10-25 12:35:35
2020-10-25 12:05:55
2020-10-25 12:06:45
'''

# 将本文件（第2周第2节课作业tmp.py）所有内容copy到 第2周第2节课作业.py 中
with open('第2周第2节课作业tmp.py', mode='rt', encoding='utf-8') as tmp:
    with open('第2周第2节课作业.py', mode='wt', encoding='utf-8') as new_file:
        for line in tmp:
            new_file.write(line)
